// Register a new CKEditor plugin.
// http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.resourceManager.html#add
CKEDITOR.plugins.add('simpleLink',
{
    // The plugin initialization logic goes inside this method.
    // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.pluginDefinition.html#init
    init: function (editor) {
        // Create an editor command that stores the dialog initialization command.
        // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.command.html
        // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialogCommand.html
        editor.addCommand('simpleLinkDialog', new CKEDITOR.dialogCommand('simpleLinkDialog'));

        // Create a toolbar button that executes the plugin command defined above.
        // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.ui.html#addButton
        editor.ui.addButton('SimpleLink',
		{
		    // Toolbar button tooltip.
		    label: 'Insert background image',
		    // Reference to the plugin command name.
		    command: 'simpleLinkDialog',
		    // Button's icon file path.
		    icon: this.path + 'images/icon.png'
		});

        // Add a new dialog window definition containing all UI elements and listeners.
        // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.html#.add
        // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.dialogDefinition.html
        CKEDITOR.dialog.add('simpleLinkDialog', function (editor) {
            return {
                // Basic properties of the dialog window: title, minimum size.
                // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.dialogDefinition.html
                title: 'Upload Background Images',
                minWidth: 400,
                minHeight: 200,
                // Dialog window contents.
                // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.definition.content.html
                contents:
				[
					{
					    // Definition of the Settings dialog window tab (page) with its id, label and contents.
					    // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.contentDefinition.html
					    id: 'general',
					    label: 'Settings',
					    elements:
						[
                            {
	                            type: 'html',
	                            html:   '<div><form id="form1" action="Upload.aspx" method="post" enctype="multipart/form-data">'
			                            + '<div class="row"><label for="fileToUpload">Select a File to Upload</label>'
			                            + '<input id="fileToUpload" type="file" name="fileToUpload" onchange="fileSelected();" /></div>'
			                        
                            }
						]
					}
				],
                buttons: [
                        CKEDITOR.dialog.cancelButton
                     ],
                onOk: function () {

                    // Create a link element and an object that will store the data entered in the dialog window.
                    // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom.document.html#createElement
                    var dialog = this,
						data = {},
						link = editor.document.createElement('a');
                    // Populate the data object with data entered in the dialog window.
                    // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.html#commitContent
                    this.commitContent(data);

                    // Set the URL (href attribute) of the link element.
                    // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom.element.html#setAttribute
                    link.setAttribute('href', data.url);

                    // In case the "newPage" checkbox was checked, set target=_blank for the link element.
                    if (data.newPage)
                        link.setAttribute('target', '_blank');
                    // Insert the Displayed Text entered in the dialog window into the link element.
                    // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom.element.html#setHtml
                    link.setHtml(data.contents);

                    // Insert the link element into the current cursor position in the editor.
                    // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#insertElement
                    editor.insertElement(link);

                    console.info(data)
                }
            };
        });
    }
});